package newsrecommendation.datamodel;

import newsrecommendation.datamodel.utils.Scores;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import newsrecommendation.utils.ListUtils;

/**
 *
 * @author Andrea
 */
public class PopularityManager {

    private HashMap<String, Double> scoreHistory;
    private static PopularityManager instance;


    private PopularityManager() {
        scoreHistory = new HashMap();
    }

    public static PopularityManager get() {
        if (instance == null) {
            instance = new PopularityManager();
        }
        return instance;
    }

    public void setClick(String idNews) {
        if (scoreHistory.containsKey(idNews)) {
            scoreHistory.put(idNews, scoreHistory.get(idNews) + Scores.CLICK);
        } else {
            scoreHistory.put(idNews, Scores.CLICK);
        }
    }

    public void setLike(String idNews) {
        if (scoreHistory.containsKey(idNews)) {
            scoreHistory.put(idNews, scoreHistory.get(idNews) + Scores.LIKE);
        } else {
            scoreHistory.put(idNews, Scores.LIKE);
        }
    }

    public List<String> getTopKNews(int K, List<NewsProduct> available) {
        List<String> keys = new ArrayList();
        List<Double> values = new ArrayList();
        //Find values for available
        for (int i = 0; i < available.size(); i++) {
            keys.add(available.get(i).getId());
            if (scoreHistory.containsKey(keys.get(i))) {
                values.add(scoreHistory.get(keys.get(i)) / available.get(i).getPublishedDays());
            } else {
                values.add(0.0);
            }
        }

        List<String> organizedKeys = ListUtils.organizeKeys(keys, values);
        if (organizedKeys.size() < K) {
            K = organizedKeys.size();
        }

        return organizedKeys.subList(0, K);
    }
}